<?php
// +----------------------------------------------------------------------
// |     【DealInfo】Service服务类
// +----------------------------------------------------------------------

declare (strict_types = 1);

namespace app\common\service;

use think\facade\Request;
use app\common\model\DealInfo as M;
use app\common\validate\DealInfo as V;

class DealInfo{
    // 添加
    public static function goAdd($data){
        //验证
        $validate = new V;
        if(!$validate->scene('add')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'add');
            $model=M::create($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }
    
    // 编辑
    public static function goEdit($data,$id){
        $data['id'] = $id;
        //验证
        $validate = new V;
        if(!$validate->scene('edit')->check($data))
        return ['msg'=>$validate->getError(),'code'=>201];
        try {
			$data=self::dataFormat($data,'edit');
             M::update($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 状态
    public static function goStatus($data,$id){
        $model =  M::find($id);
        if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
        try{
            $model->save($data);
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 删除
    public static function goRemove($id,$did=false){
        try{
			if($did){	//删除订单时删除子明细
				M::where('did',$id)->delete();
			}else{
				$model = M::find($id);
				if ($model->isEmpty()) return ['msg'=>'数据不存在','code'=>201];
				$model->delete();				
			}
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 批量删除
    public static function goBatchRemove($ids,$did=false){
        try{
			if($did){	//删除订单时删除子明细
				M::destroy(function($query){
					$query->where('did',$ids);
				});
			}else{
				if (!is_array($ids)) return ['msg'=>'数据不存在','code'=>201];
				M::destroy($ids);
			}
        }catch (\Exception $e){
            return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
        }
    }

    // 获取列表
    public static function goRecycle(){
        if (Request::isPost()){
            $ids = Request::param('ids');
            if (!is_array($ids)) return ['msg'=>'参数错误','code'=>'201'];
            try{
                if(Request::param('type')){
                    $data = M::onlyTrashed()->whereIn('id', $ids)->select();
                    foreach($data as $k){
                        $k->restore();
                    }
                }else{
                    M::destroy($ids,true);
                }
            }catch (\Exception $e){
                return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
            }
            return ['msg'=>'操作成功'];
        }
        //按用户名
        $where = [];
        $limit = I('get.limit/d');
        
        $list = M::onlyTrashed()->where($where)->paginate($limit);
        return ['code'=>0,'data'=>$list->items(),'extend'=>['count' => $list->total(), 'limit' => $limit]];
    }
	
	//修改某个参数
	public static function setFieldValue($data){
		$model =  M::find($data['id']);
		if ($model->isEmpty())  return ['msg'=>'数据不存在','code'=>201];
		try{
		    $model->save([
		        $data['field'] => $data['value'],
		    ]);
			return ['msg'=>'操作成功'];
		}catch (\Exception $e){
		    return ['msg'=>'操作失败'.$e->getMessage(),'code'=>201];
		}
	}
	
	//批量添加订单明细
	public static function write_info($info,$did,$uuid=0){
		foreach ($info['cloid'] as $i => $val) {
			if($info['cloid'][$i]!=''){
				$data=[
					'cloid'=>$info['cloid'][$i],
					'number'=>$info['number'][$i],
					'name'=>$info['name'][$i],
					'color'=>$info['color'][$i],
					'colorname'=>$info['colorname'][$i],
					'colorval'=>$info['colorval'][$i],
					'onumber'=>$info['onumber'][$i],
					'ocolorid'=>$info['ocolorid'][$i],
					'gray'=>$info['gray'][$i],
					'weight'=>$info['weight'][$i],
					'unit'=>$info['unit'][$i],
					'reduce'=>$info['reduce'][$i]==''?0:$info['reduce'][$i],
					'count'=>$info['count'][$i],
					'roll'=>$info['roll'][$i],
					'unitprice'=>$info['unitprice'][$i],
					'total'=>$info['total'][$i],
					'bacth'=>$info['bacth'][$i],
					'uuid'=>$uuid,
				];
				if($info['infoid'][$i]==""){
					$data['did']=$did;
					self::goAdd($data);
				}else{
					$id=$info['infoid'][$i];
					if($data['number']=='' && $data['name']==''){
						self::goRemove($id);
					}else{
						self::goEdit($data,$id);
					}
				}
			}
		}
	}
	
	// 预处理添加和修改的数据
	public static function dataFormat($data,$type='add'){
		if($type == 'add'){					//添加时才处理

		}else if($type == 'edit'){			//修改时才处理
			
		}
		$data['weight']=serialize($data['weight']);
		return $data;
	}
}
